import 'dart:io';
import 'dart:typed_data';
import 'package:convert/convert.dart' as convert;
import 'package:crypto/crypto.dart';


class Utils{
  static const int partSize = 1024 * 1024 * 3; //默认3m每块

  static String _getMd5(String path) {
    File file = File(path);
    int fileSize = file.lengthSync();
    int totalPart = (fileSize * 1.0 / partSize).ceil();

    int start; //开始读文件的位置
    int length; //读取文件的长度
    var output = convert.AccumulatorSink<Digest>();
    var input = md5.startChunkedConversion(output);

    int currentPart = 0;
    while (currentPart < totalPart) {
      start = currentPart * partSize;
      length = (start + partSize > fileSize) ? (fileSize - start ) : partSize;

      RandomAccessFile raf = file.openSync(mode: FileMode.read);
      raf.setPositionSync(start);
      Uint8List data = raf.readSync(length);
      input.add(data);
      currentPart++;
    }

    input.close();
    var digest = output.events.single.toString();
    return digest;
  }
}